<?php 
class ControllerContact extends Controller {
	var $error = array(); 
	    
  	function index() {
		$config   =& $this->locator->get('config');
		$language =& $this->locator->get('language');
		$module   =& $this->locator->get('module');
		$response =& $this->locator->get('response');
		$request  =& $this->locator->get('request');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
			
		$language->load('controller/contact.php');

    	$template->set('title', $language->get('heading_title'));  
	 
    	if ($request->isPost() && $request->has('email', 'post') && $this->validate()) {
			$mail = $this->locator->create('mail');
	  		
			$mail->setTo($config->get('config_email'));
	  		$mail->setFrom($request->get('email', 'post'));
	  		$mail->setSender($request->get('name', 'post'));
	  		$mail->setSubject($language->get('email_subject', $request->get('name', 'post')));
	  		$mail->setText($request->get('enquiry', 'post'));
      		$mail->send();

	  		$response->redirect($url->ssl('contact', 'success'));
    	}

    	$view = $this->locator->create('template');

		$view->set('tpl', $template);

    	$view->set('heading_title', $language->get('heading_title'));

    	$view->set('text_address', $language->get('text_address'));
    	$view->set('text_telephone', $language->get('text_telephone'));
    	$view->set('text_fax', $language->get('text_fax'));

    	$view->set('entry_name', $language->get('entry_name'));
    	$view->set('entry_email', $language->get('entry_email'));
    	$view->set('entry_enquiry', $language->get('entry_enquiry'));

    	$view->set('error_name', @$this->error['name']);
    	$view->set('error_email', @$this->error['email']);
    	$view->set('error_enquiry', @$this->error['enquiry']);

    	$view->set('button_continue', $language->get('button_continue'));
    
		$view->set('action', $url->href('contact'));
    
		$view->set('store', $config->get('config_store'));

    	$view->set('address', nl2br($config->get('config_address')));

    	$view->set('telephone', $config->get('config_telephone'));

    	$view->set('fax', $config->get('config_fax'));

    	$view->set('name', $request->gethtml('name', 'post'));

    	$view->set('email', $request->gethtml('email', 'post'));

    	$view->set('enquiry', $request->gethtml('enquiry', 'post'));

		$template->set('content', $view->fetch('content/contact.tpl'));
	
		$template->set($module->fetch());
	
    	$response->set($template->fetch('layout.tpl'));
  	}

  	function success() {
		$language =& $this->locator->get('language');
		$module   =& $this->locator->get('module');
		$response =& $this->locator->get('response');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');
		
		$language->load('controller/contact.php');

    	$template->set('title', $language->get('heading_title'));  
		
    	$view = $this->locator->create('template');

			$view->set('tpl', $template);

    	$view->set('heading_title', $language->get('heading_title'));

    	$view->set('text_success', $language->get('text_success'));

    	$view->set('button_continue', $language->get('button_continue'));

    	$view->set('continue', $url->href('home'));

		$template->set('content', $view->fetch('content/success.tpl'));
	
		$template->set($module->fetch());
	
    	$response->set($template->fetch('layout.tpl'));
  }
    
  	function validate() {
		$language =& $this->locator->get('language');
		$request  =& $this->locator->get('request');
		$validate =& $this->locator->get('validate');

		if (!$validate->strlen($request->get('name', 'post'),3,32)) {
      		$this->error['name'] = $language->get('error_name');
    	}

    	if (!$validate->email($request->get('email', 'post'))) {
      		$this->error['email'] = $language->get('error_email');
    	}

    	if (!$validate->strlen($request->get('enquiry', 'post'),10,1000)) {
      		$this->error['enquiry'] = $language->get('error_enquiry');
    	}
	 
		if (!$this->error) return TRUE; 	  
  	}
}
?>
